package xyz.yeidi.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import xyz.yeidi.pojo.Orders;
import xyz.yeidi.service.api.OrdersService;
import xyz.yeidi.utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author JIP
 * @Date 2021-08-07 19:00
 * @Version 1.0
 * @Describe :
 */
@WebServlet("/addOrders")
public class AddOrdersServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrdersService ordersService = ServiceFactory.getOrdersService();
        ObjectMapper om = new ObjectMapper();
        PrintWriter out = response.getWriter();
        String userId = request.getParameter("userId");
        String businessId = request.getParameter("businessId");
        String orderDate = request.getParameter("orderDate");
        String orderTotal = request.getParameter("orderTotal");
        String daId = request.getParameter("daId");
        Orders order = new Orders();
        order.setUserId(Integer.valueOf(userId));
        order.setBusinessId(Integer.valueOf(businessId));
        order.setDaId(Integer.valueOf(daId));
        order.setOrderTotal(Double.parseDouble(orderTotal));
        order.setOrderDate(orderDate);
        boolean b = ordersService.addOrders(order);
        if(b){
            out.print(om.writeValueAsString(ordersService.getOrderIdByUserId(Integer.valueOf(userId))));
        }else{
            out.print("error");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
